﻿using Moq;
using System;
using System.Linq;
using System.Web.Mvc;
using Stimulus360.Services;
using Stimulus360.Models.Data;
using System.Collections.Generic;
using Stimulus360.Controllers.Users;
using Stimulus360.Tests.Repositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Stimulus360.Tests.Controllers
{
    /// <summary>
    ///    This class contains test methods to test the Users Controller
    /// </summary>
    [TestClass]
    public class UsersControllerTest
    {
        private TestContext _testContextInstance;

        /// <summary>
        ///     Gets or sets the test context which provides
        ///     information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        /// <summary>
        ///     Method to create  dependency injected Users Controller
        /// </summary>
        /// <returns>
        ///     UserController Controller with a UserService injected with fake repository
        /// </returns>
        private static UsersController  CreateUsersController()
        {
            List<Idea> testIdeas = FakeData.CreateIdeas() ;
            var repository = new FakeDataRepository(testIdeas, null);
            UserService userService = new UserService(null, repository, null);

            UsersController controller = new UsersController(userService, null);
            return controller;
        }

        /// <summary>
        ///     Method to create a dependency injected Users Controller with the HTTPContext user
        ///     and authorization set.
        /// </summary>
        /// <param name="userName">
        ///     The username to set in the HTTP context
        /// </param>
        /// <returns>
        ///     Dependency injected UsersController with the HTTPContext user
        ///     and authorization set.
        /// </returns>
        private static UsersController  CreateUsersControllerAs(string userName)
        {

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName);
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = CreateUsersController();
            controller.ControllerContext = mock.Object;

            return controller;
        }

        /// <summary>
        ///    Test method to get the list of ideas submitted by a given user.
        /// </summary>
        [TestMethod]
        public void SubmittedIdeasTest()
        {
            UsersController target =CreateUsersControllerAs("Admin");
            Nullable<int> page = new Nullable<int>();
            var result = (ViewResult)target.Ideas(page);
            Assert.IsInstanceOfType(result.ViewData.Model, typeof(IdeaListViewModel));
            IdeaListViewModel viewModel = (IdeaListViewModel)result.ViewData.Model;
            var submittedByFail = from idea in viewModel.Ideas
                                  where idea.SubmittedBy != "Admin"
                                  select true;
            Assert.IsFalse(submittedByFail.FirstOrDefault());

          

        }
    }
}